package ar.uba.fi.taller2.TreesWeb.ui;

import java.util.Iterator;
import java.util.List;
import java.util.Map;


import org.zkoss.zhtml.Messagebox;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Tree;
import org.zkoss.zul.Treechildren;
import org.zkoss.zul.Treeitem;
import org.zkoss.zul.Window;

public abstract class DefaultWindow extends Window
{
	public abstract void onCreate();
	public abstract void onOK() throws Exception;
	
	public void onCancel() 
	{	
		Component C = this.getParent();
		this.detach();		
		C.invalidate();
	}
	
	protected String getTextboxValue(String name)
	{
		return getTextboxValue(this, name);
	}
	
	protected String getTextboxValue( Component c, String name)
	{
		Textbox ctrl = (Textbox) c.getFellow(name);
		String value = ctrl.getValue();
		return value;
	}
	
	protected String getLabelValue( Component c, String name)
	{
		Label ctrl = (Label) c.getFellow(name);
		String value = ctrl.getValue();
		return value;
	}
	
	protected String getLabelValue( String name)
	{
		return getLabelValue(this,name);
	}
	
	protected String getComboboxValue(String name)
	{
		Combobox ctrl = (Combobox) this.getFellow(name);
		String value = ctrl.getValue();
		String id = null;
	
		for (int i = 0; i < ctrl.getItemCount() && id == null; i++)
		{
			Comboitem comboitem = ctrl.getItemAtIndex(i);
			
			if ( comboitem.getLabel().equals( value ) )
			{
				id = (String) comboitem.getAttribute("value");
			}
		}
			
		return id;
	}

	protected void setTextboxValue(Component c, String name, String value)
	{
		setTextboxValue(c, name, value, true);
	}
	
	protected void setTextboxValue(String name, String value)
	{
		setTextboxValue(name, value, true);
	}

	protected void setTextboxValue(String name, String value, boolean enabled)
	{
		setTextboxValue(this, name, value, enabled);
	}
	
	protected void setTextboxValue(Component c, String name, String value, boolean enabled)
	{
		Textbox ctrl = (Textbox) c.getFellow(name);
		ctrl.setValue( value );
		ctrl.setDisabled(!enabled);
	}
	
	protected void setComboboxValue(String name, String value)
	{
		setComboboxValue(name, value, true);
	}

	protected void setComboboxValue(String name, String value, boolean enabled)
	{
		Combobox ctrl = (Combobox) this.getFellow(name);
		
		for (int i = 0; i < ctrl.getItemCount() ; i++)
		{
			Comboitem comboitem = ctrl.getItemAtIndex(i);
			
			if ( ((String) comboitem.getAttribute("value")).equals( value ) )
			{
				ctrl.setValue( comboitem.getLabel() );
			}
		}

		ctrl.setDisabled(!enabled);
	}

	protected Listitem getSelectedItem( Component c, String listName)
	{
		Listbox lb = (Listbox)c.getFellow(listName);
		Listitem lt = lb.getSelectedItem();
		if (lt == null)
			lt = lb.getItemAtIndex(0);
		return lt;
	}
	
	protected Listitem getSelectedItem(String listName)
	{
		return getSelectedItem(this,listName);
	}
	
/*	protected void setSelectedItem(String listName, Long id)
	{
		setSelectedItem(this, listName, id);
	}*/
	
/*
	protected void setSelectedItem(Component c, String listName, Long id)
	{
		Listbox lb = (Listbox)c.getFellow(listName);
		Iterator listitems = lb.getItems().iterator();
		while ( listitems.hasNext() )
		{
			Listitem li = (Listitem) listitems.next();
			DomainObject domainObject = (DomainObject) li.getValue(); 
			if ( domainObject.getId().equals(id) )
			{
				li.setSelected(true);
				break;
			}
		}
	}
*/	
	protected void setSelectedItem(String listName, int idx)
	{
		Listbox lb = (Listbox)this.getFellow(listName);
		lb.setSelectedIndex( idx );
	}
	
	protected void setSelectedItem(String listName, String code)
	{
		Listbox lb = (Listbox)this.getFellow(listName);
		Iterator listitems = lb.getItems().iterator();
		while ( listitems.hasNext() )
		{
			Listitem li = (Listitem) listitems.next();
			String selCode = (String) li.getValue(); 
			if (code.equals( selCode ) )
			{
				li.setSelected(true);
				break;
			}
		}
	}

	protected void setSelectedItemByLabel(String listName, String label)
	{
		Listbox lb = (Listbox)this.getFellow(listName);
		Iterator listitems = lb.getItems().iterator();
		while ( listitems.hasNext() )
		{
			Listitem li = (Listitem) listitems.next();
			String selLabel = (String) li.getLabel(); 
			if (label.equals( selLabel ) )
			{
				li.setSelected(true);
				break;
			}
		}
	}
	
	
/*	protected void setSelectedItem(String listName, DomainObject domainObject)
	{
		Listbox lb = (Listbox)this.getFellow(listName);
		Iterator listitems = lb.getItems().iterator();
		while ( listitems.hasNext() )
		{
			Listitem li = (Listitem) listitems.next();
			DomainObject selDO = (DomainObject) li.getValue(); 
			if ( selDO != null && selDO.getId().equals( domainObject.getId() ) )
			{
				li.setSelected(true);
				break;
			}
		}
	}*/
	
	protected Window newWindow( String uri, Map params )
	{
		return newWindow( uri, null, params );	
	}
	
	protected Window newWindow( String uri, Component parent, Map params )
	{
		return (Window) Executions.createComponents(uri, parent, params);	
	}		
}
